package com.example.icongyou.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.icongyou.R;
import com.example.icongyou.bean.TeamMember;
import java.util.List;

public class TeamMemberAdapter extends RecyclerView.Adapter<TeamMemberAdapter.MemberViewHolder> {

    private final List<TeamMember> memberList;

    public TeamMemberAdapter(List<TeamMember> members) {
        this.memberList = members;
    }

    @NonNull
    @Override
    public MemberViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_team_member, parent, false);
        return new MemberViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MemberViewHolder holder, int position) {
        TeamMember member = memberList.get(position);
        holder.tvMemberName.setText(member.getStudentName());
        holder.tvMemberRole.setText(member.getRole());
        holder.pbContribution.setProgress((int) member.getContribution());
        holder.tvContribution.setText(String.format("%.1f%%", member.getContribution()));

        // 为队长设置特殊样式
        if ("LEADER".equals(member.getRole())) {
            holder.tvMemberRole.setBackgroundResource(R.drawable.shape_leader_tag);
        } else {
            holder.tvMemberRole.setBackgroundResource(R.drawable.shape_member_tag);
        }
    }

    @Override
    public int getItemCount() {
        return memberList.size();
    }

    public static class MemberViewHolder extends RecyclerView.ViewHolder {
        TextView tvMemberName;
        TextView tvMemberRole;
        ProgressBar pbContribution;
        TextView tvContribution;

        public MemberViewHolder(@NonNull View itemView) {
            super(itemView);
            tvMemberName = itemView.findViewById(R.id.tv_member_name);
            tvMemberRole = itemView.findViewById(R.id.tv_member_role);
            pbContribution = itemView.findViewById(R.id.pb_contribution);
            tvContribution = itemView.findViewById(R.id.tv_contribution);
        }
    }
}